﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manager_System
{
    public partial class add_ingot : Form
    {
        string type = "";

        string Id = "";
        main_form form1;
        string Interid = "";
        public add_ingot(main_form form)
        {
            type = "添加";
            form1 = form;
            InitializeComponent();
            cost_tb.MaxLength = 100;
    
          
        }

        public add_ingot(string id,string potnum,string cost,string weight,string num,string note,string material,string interid, main_form form)
        {
            InitializeComponent();
            type = "修改";
            Id = id.Substring(0,4)+"/"+id.Substring(4,2)+"/"+id.Substring(6,2);
            produce_dt.Value = Convert.ToDateTime(Id);
            potnum_tb.Text = potnum;
            cost_tb.Text = cost;
            weight_tb.Text = weight;
            num_tb.Text = num;
            note_tb.Text = note;
            material_tb.Text = material;
            form1 = form;
                cost_tb.MaxLength = 100;
            produce_dt.Enabled = false;
            Interid = interid;
        }



        private void add_produce_Load(object sender, EventArgs e)
        {
            produce_dt.Format = DateTimePickerFormat.Custom; //设置为显示格式为自定义
            produce_dt.CustomFormat = "yyyy-MM-dd"; 
        }

        private void produce_tb_TextChanged(object sender, EventArgs e)
        {
            text_num.Text = "您还可以输入" + (50 - cost_tb.Text.Length).ToString() + "个汉字";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            #region 添加/修改计划
            string sql = "";
            string ID = produce_dt.Text.Substring(0, 4) + produce_dt.Text.Substring(5, 2) + produce_dt.Text.Substring(8, 2);
            if (type.Equals("添加"))
            {
                sql = "insert into T_ingot (ID,PotNum,Cost,Weight,Num,Note,Material) values('" + ID + "','" + this.potnum_tb.Text + "','" + cost_tb.Text.Trim() + "','" + weight_tb.Text + "','" + num_tb.Text + "','" + note_tb.Text + "','" + material_tb.Text + "')";
            }
            if (type.Equals("修改"))
            {
                sql = "update T_ingot set Cost='" + this.cost_tb.Text.Trim() + "',PotNum='"+ potnum_tb.Text +"',Weight='"+weight_tb.Text+"',Num='"+note_tb.Text+"',Note='"+note_tb.Text+"',Material='"+material_tb.Text+"'where InterID='" + Interid + "'";
            }

            if (cost_tb.Text.Trim() != String.Empty && potnum_tb.Text.Trim() != String.Empty && weight_tb.Text.Trim() != String.Empty && num_tb.Text.Trim() != String.Empty && note_tb.Text.Trim() != String.Empty && material_tb.Text.Trim() != String.Empty)
            {
                DBhelp db = new DBhelp();
                int rs = db.update(sql);
                if (rs == 1)
                {
                    MessageBox.Show(type.ToString() + "成功！");
                }
                else
                {
                    MessageBox.Show(type.ToString() + "失败！");
                }
                db.close();
                form1.Refresh_ingot_LV();
            }
            else
            {
                MessageBox.Show("请输入相关数值！");
            }

            #endregion
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void note_tb_TextChanged(object sender, EventArgs e)
        {
            note_size.Text = "您还可以输入" + (50 - note_tb.Text.Length).ToString() + "个汉字";
        }
    }
}
